<?php
class BaseController extends Controller {
// 	public function CheckUserLogin($callback = false) {
// 		$user = $this->getUtil ('session')->get('loginuser');
// 		if (empty ($user)) {
// 			if ($callback) {
// 				return $callback ();
// 			} else {
// 				header('location:'.$this->getUrl('membercenter', 'login', 'run'));
// 				exit();
// 			}
// 		} else {
// 			return $user ['id'];
// 		}
// 	}

	
	
	

	/**
	 * 如果POST操作都需要带Token
	 */
	public function isPostAction() {
		$isToken = $this->controller->check_token();
		if (!$isToken)  {
			return $this->controller->ajax_return(0, '非法Token！');
		}
	}

	/**
	 * 后台模板页面错误提示
	 *
	 * @param string $msg
	 *        	错误信息
	 */
	public function error($msg) {
		$error = $this->getUtil ( 'error' );
		$error->send_error ( $msg, 'html' );
	}
	/**
	 * 系统提示信息
	 *
	 * @access      public
	 * @param       string      msg_detail      消息内容
	 * @param       int         msg_type        消息类型， 0消息，1错误，2询问
	 * @param       array       links           可选的链接
	 * @param       boolen      $auto_redirect  是否需要自动跳转
	 * @return      void
	 */
	public function sys_msg($msg_detail, $msg_type = 0, $links = array(), $auto_redirect = true){
		if (count($links) == 0){
			$links[0]['text'] = '如果页面不能自动跳转请点击此处';
			$links[0]['href'] = 'javascript:history.go(-1)';
		}
		$this->view->assign('ur_here',     '系统消息');
		$this->view->assign('msg_detail',  $msg_detail);
		$this->view->assign('msg_type',    $msg_type);
		$this->view->assign('links',       $links);
		$this->view->assign('default_url', $links[0]['href']);
		$this->view->assign('auto_redirect', $auto_redirect);

		$this->view->display('common/message');exit;
	}


	/**
	 * 后台AJAX操作统一返回值函数
	 *
	 * @param string $status
	 *        	状态
	 * @param string $msg
	 *        	错误信息
	 */
	public function ajax_return($status, $msg, $data = array(), $type = 'json') {
		/*
		 * $data = array( 'username' => $this->userInfo['username'], 'ip' => $this->controller->get_ip(), 'controller' => $this->controller->get_gp('c'), 'action' => $this->controller->get_gp('a'), 'msg' => $msg, 'data' => $this->controller->filter_escape($_POST) );
		 */
		return $this->controller->ajax_return ( $status, $msg ,$data,$type);
	}

	/**
	 * 取出模版内容
	 */
	public function fetch_tpl($template) {
		$this->view->set_tpl($template);
		ob_start();
		$this->view->display();
		$content = ob_get_contents();
		ob_end_clean();
		return $content;
	}

	public function after() {
		$InitPHP_conf = InitPHP::getConfig ();

		$user = $this->getUtil ('session')->get('loginuser');

		$m = $this->controller->get_gp ( 'm' );
		$c = $this->controller->get_gp ( 'c' );
		$a = $this->controller->get_gp ( 'a' );

		if ($a != 'login') {

			if ($m=='membercenter') {
				$powerlist = InitPHP::getDao ( "power" )->getPowerByUserid ( $user ['id'],1 );
				$this->view->assign ( 'powerlist', $powerlist );
				$this->view->assign ( 'indexDefault', $powerlist [0] ['power_url'] );

				$this->view->set_tpl ( 'common/header', 'F' );
				$this->view->set_tpl('common/footer', 'L');
			}elseif ($m=='agent'){
				
				
				$this->view->assign("is_session", $this->getUtil('session')->get('loginuser'));
				$this->view->assign("login", $this->getUrl('membercenter', 'login', 'run'));
				$this->view->assign("registration", $this->getUrl('agent', 'registration', 'registration'));
				
				$this->view->assign ( 'user_name', $user ['user_name'] );
				$this->view->assign ( 'lastlogintime', $user ['last_login_time'] );
				$this->view->assign('quit', $this->getUrl('membercenter', 'login', 'userquit'));

				$this->view->set_tpl ( 'common/header_agent', 'F' );
				$this->view->set_tpl('common/footer_agent', 'L');
			}
		}
		$this->view->assign ('base_url', $InitPHP_conf['url']);

		$this->view->display ();
	}

	/**
	 * 后台公用URL组装函数
	 *
	 * @param string $c
	 * @param string $a
	 */
	public function getUrl($m, $c, $a) {
		$config = InitPHP::getConfig ();
		return InitPHP::url ( $m . '|' . $c . '|' . $a );
	}

	/**
	 * 日期处理
	 */
	public function _getDateTime($dates) {
		if ($dates == '')
		return 0;
		$dtime [0] = substr ( $dates, 0, 10 );
		$dtime [1] = substr ( $dates, - 8, 8 );
		$date = explode ( '-', $dtime [0] );
		$time = explode ( ':', $dtime [1] );
		return mktime ( $time [0], $time [1], $time [2], $date [1], $date [2], $date [0] );
	}

	/**
	 * 生成表单tokey
	 */
	function createToken($uniqueid) {
		$common = InitPHP::loadclass ( 'baseMethod' );
		
		$uniqueid = empty($uniqueid) ? $common->rand_str(8) : $uniqueid;
		
		$token = md5("qbs_check_repeat_" . $uniqueid);
		
		$this->getUtil ( 'session' )->set ( 'form_token',$token );

		return $token;
	}

	/**
	 * 检查表单tokey
	 */
	function checkToken($token) {
		$form_token = $this->getUtil ( 'session' )->get ( 'form_token');
		if (!isset($form_token) || empty($form_token) || $form_token != $token) {
			return false;
		} else {
			$this->getUtil ( 'session' )->del ( 'form_token');
			return true;
		}
	}
}